package com.ly.loginstarter;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.sql.DataSource;

@Configuration
@ComponentScan("com.ly.loginstarter")
// 检查配置属性是否有某个值
@ConditionalOnProperty(prefix = "ly.login", name = "enable")
// 检查类路径中是否包含某个类是否有某个值
// 检查数据源
@ConditionalOnClass(DataSource.class)
public class LoginConfig {

    @PostConstruct
    public void init(){
        System.out.println("=========== login-starter 启动成功! ===========");
    }

    // 注入容器中现有的 数据源
    @Resource
    DataSource dataSource;

    @Bean
    // 如果 IOC 容器中没有 JdbcTemplate , 那么才创建
    @ConditionalOnMissingBean(JdbcTemplate.class)
    JdbcTemplate jdbcTemplate(){
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        return jdbcTemplate;
    }

}
